package web.zz.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import web.zz.bean.User;

@Repository
public class UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

//    public int getMatchCOunt(String username,String password){
//        String sqlStr = "select count(*) from tb_user where username=? and password=?";
//        return jdbcTemplate.queryForObject(sqlStr,new Object[]{username,password},Integer.class);
//    }
//
//    public User findUser(final String username){
//        String sqlStr = "select id from tb_user where username=?";
//        final User user = new User();
//        jdbcTemplate.query(sqlStr, new Object[]{username},
//                new RowCallbackHandler() {
//                    @Override
//                    public void processRow(ResultSet resultSet) throws SQLException {
//                        user.setId(resultSet.getInt("id"));
//                        user.setUsername(username);
//                    }
//                });
//        return user;
//    }
    public User findUser(String username){
        String sql = "SELECT*FROM tb_user WHERE username=?";
        RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);
        try {
            return jdbcTemplate.queryForObject(sql,new Object[]{username},rowMapper);
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }
}
